function handleEvent()
{
  var oField = document.getElementById('bodyId');
  window.external.HandleEvent( 
      oField.document.queryCommandEnabled( 'Cut' ),
      oField.document.queryCommandEnabled( 'Copy' ),
      oField.document.queryCommandEnabled( 'Paste' ),
      oField.document.queryCommandEnabled( 'Undo' ),
      oField.document.queryCommandEnabled( 'Redo' ),
      oField.document.queryCommandState( 'Bold' ),
      oField.document.queryCommandState( 'Italic' ),
      oField.document.queryCommandState( 'Underline' ),
      oField.document.queryCommandState( 'JustifyLeft' ),
      oField.document.queryCommandState( 'JustifyCenter' ),
      oField.document.queryCommandState( 'JustifyRight' ),
      oField.document.queryCommandState( 'InsertOrderedList' ),
      oField.document.queryCommandState( 'InsertUnorderedList' ),
      oField.document.queryCommandEnabled( 'InsertImage' ),
      oField.document.queryCommandEnabled( 'CreateLink' ),
      oField.document.queryCommandValue( 'FormatBlock' )
  );
  if( event.type == 'dblclick' ) oField.focus();
}

function insertCode()
{
  var sel = document.selection.createRange()
  if( sel.text != '' )
  {
     document.execCommand( 'FormatBlock', false, '<PRE>' )
  }else{
    var oPre = document.createElement('PRE');
    oPre.innerText = '<Insert Code Here>';
    var sel = document.selection.createRange()
    sel.parentElement().appendChild( oPre );
    sel.findText( '<Insert Code Here>' );
    sel.select();
  }
}

function setHeading( type )
{
  var sel = document.selection.createRange()
  if( sel.text != '' )
  {
     document.execCommand( 'FormatBlock', false, '<H' + type + '>' )
  }else{
    alert( 'Please select the text you want to convert to Heading ' + type );
  }
}

function setNormal()
{
  var sel = document.selection.createRange()
  if( sel.text != '' )
  {
     document.execCommand( 'FormatBlock', false, 'Normal' )
  }else{
    alert( 'Please select the text you want to convert to Normal' );
  }
}

function getHref( href, text )
{
  var sel = document.selection.createRange();
  var parent = sel.parentElement();
  return parent.href;
}

function insertLink( href, text )
{
  var sel = document.selection.createRange();
  if( sel.text != '' )
  {
    document.execCommand( 'CreateLink', false, href )
  }else{
    var a = document.createElement('A');
    a.href = href;
    a.innerText = text;
    sel.parentElement().appendChild( a );
  }
}

function insertTable( rows, cols )
{
  var sel = document.selection.createRange();
  if( sel.text == '' )
  {
    var table = document.createElement('TABLE');
    table.border = 1;
    table.className = 'userTable';
    var tbody = document.createElement('TBODY');
    for( var row = 0; row < rows; row++ )
    {
       var tr = document.createElement('TR');
       for( var col = 0; col < cols; col++ )
       {
          var td = document.createElement('TD');
          td.className = 'userTable';
          if( col == 0 && row == 0 ) td.innerText = '<Insert table text>';
          tr.appendChild( td );
       }
       tbody.appendChild( tr );
    }
    table.appendChild( tbody );
    sel.parentElement().appendChild( table );
    sel.findText( '<Insert table text>' );
    sel.select();
  }
}

function showError( word )
{
    var range = document.body.createTextRange();
    if( range.findText( word, null, 2 ) )
    {
        range.select();
    }
}

function replaceText(search, replace) {
    if (search) {
        var rng = document.body.createTextRange();
        for (var i = 0; rng.findText(search, null, 2); i++) {
            rng.text = replace;
            rng.collapse(false)  ;   
        }
    }
}